package com.simon.hse.activity.contractor;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bumptech.glide.Glide;
import com.donkingliang.imageselector.utils.ImageSelector;
import com.donkingliang.imageselector.utils.ImageSelectorUtils;
import com.google.gson.Gson;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.activity.SignActivity;
import com.simon.hse.adapter.CommonAdapter;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.Constants;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.AddViolationInput;
import com.simon.hse.http.entity.CommonOutput;
import com.simon.hse.http.entity.ContractorCompanyOutput;
import com.simon.hse.http.entity.ContractorPersonOutput;
import com.simon.hse.http.entity.DropDownInput;
import com.simon.hse.http.entity.DropDownOutput;
import com.simon.hse.http.entity.TrainPersonOutput;
import com.simon.hse.http.entity.UploadFileOutput;
import com.simon.hse.http.entity.ViolationDetailOutput;
import com.simon.hse.http.entity.ViolationManagerOutput;
import com.simon.hse.utils.BitmapUtils;
import com.simon.hse.utils.DateTimeUtils;
import com.simon.hse.utils.PopupWindowUtils;
import com.simon.hse.utils.SelectPicUtils;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 违章录入
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_add_violation)
public class AddViolationActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.ll_violation_name)
    LinearLayout mLl_violation_name;

    @ViewInject(R.id.tv_violation_name)
    TextView mTv_violation_name;

    @ViewInject(R.id.ll_violation_person)
    LinearLayout mLl_violation_person;

    @ViewInject(R.id.tv_violation_person)
    TextView mTv_violation_person;

    @ViewInject(R.id.ll_violation_date)
    LinearLayout mLl_violation_date;

    @ViewInject(R.id.tv_violation_date)
    TextView mTv_violation_date;

    @ViewInject(R.id.ll_violation_find_person)
    LinearLayout mLl_violation_find_person;

    @ViewInject(R.id.tv_violation_find_person)
    TextView mTv_violation_find_person;

    @ViewInject(R.id.et_violation_clause)
    TextView mEt_violation_clause;

    @ViewInject(R.id.et_violation_introduce)
    EditText mEt_violation_introduce;

    @ViewInject(R.id.et_violation_remark)
    EditText mEt_violation_remark;

    @ViewInject(R.id.et_violation_idea)
    EditText mEt_violation_idea;

    @ViewInject(R.id.tv_violation_sign)
    TextView mTv_violation_sign;

    @ViewInject(R.id.common_recyclerview)
    RecyclerView mRecyclerView;

    @ViewInject(R.id.ll_violation_blacklist)
    LinearLayout mLl_violation_blacklist;

    @ViewInject(R.id.ll_zongjignli)
    LinearLayout mLl_zongjignli;

    @ViewInject(R.id.tv_violation_blacklist)
    TextView mTv_violation_blacklist;

    @ViewInject(R.id.rv_pic)
    RecyclerView mRv_pic;
    @ViewInject(R.id.tv_fujian_name)
    TextView tvFujianName;
    @ViewInject(R.id.tv_fujian_time)
    TextView tvFujianTime;
    @ViewInject(R.id.tv_sign_time)
    TextView tv_sign_time;

    private ViolationManagerOutput.DataBean.PageDataBean pageDataBean;
    private String id, cbsListId, violatorsId, discoveringPeopleId, processingResultId;
    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<Bitmap> mListSign = new ArrayList<>();// 签名图片
    private List<DropDownOutput.DataBean.DataDicListBean.ValueBean> mBlackList = new ArrayList<>();// 黑名单列表
    //上传图片
    private String trainCourseId, trainPersonId, participantName, participantId;
    private CommonAdapter mCommonAdapter;
    private List<String> mPicList = new ArrayList<>();
    private List<String> mPicIdList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPicRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left})
    private void finish(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                setResult(RESULT_OK, new Intent());
                finish();
                break;

        }
    }

    @Event(value = {R.id.ll_violation_name, R.id.ll_violation_person, R.id.ll_violation_date, R.id.ll_violation_find_person, R.id.ll_violation_blacklist
            , R.id.tv_violation_sign, R.id.tv_violation_cancel, R.id.tv_violation_confirm, R.id.iv_add_train_record_pic, R.id.et_violation_clause})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.ll_violation_name:
                initContractorCompanyPop();
                break;
            case R.id.ll_violation_person:
                initTrainPersonPop();
                break;
            case R.id.ll_violation_date:
                initViolationTime();
                break;
            case R.id.ll_violation_find_person:
                initFindPersonPop();
                break;
            case R.id.tv_violation_sign:
                Intent intent = new Intent(AddViolationActivity.this, SignActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.ll_violation_blacklist:
                initBlackListPop();
                break;
            case R.id.tv_violation_cancel:
                finish();
                break;
            case R.id.tv_violation_confirm:
                submit();
                break;
            case R.id.iv_add_train_record_pic:
                SelectPicUtils.takePic(AddViolationActivity.this, SelectPicUtils.REQUEST_CODE_CHOOSE);
                ImageSelector.builder()
                        .useCamera(true) // 设置是否使用拍照
                        .setSingle(true)  //设置是否单选
                        .start(AddViolationActivity.this, Constants.REQUEST_CODE); // 打开相册
                break;
            case R.id.et_violation_clause:
                Intent intentClause = new Intent(AddViolationActivity.this, ViolationItemActivity.class);
                startActivity(intentClause);
                break;
        }
    }

    private void initData() {
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            mTv_common_title.setText("违章录入");
            mTv_violation_date.setText(DateTimeUtils.getCurrentYMD());
        } else {
            mTv_common_title.setText("违章修改");
            pageDataBean = (ViolationManagerOutput.DataBean.PageDataBean) extras.getSerializable("RECORD_DATA");

            id = pageDataBean.getId();
            initExaminDetail();
            String violationDate = pageDataBean.getViolationDate();
            if (violationDate.contains(" ")) {
                String[] split = violationDate.split(" ");
                violationDate = split[0];
            }
            mTv_violation_date.setText(violationDate);
            mTv_violation_find_person.setText(pageDataBean.getDiscoveringPeople());
            mEt_violation_introduce.setText(pageDataBean.getViolationInstructions());
        }

        initBlackList();
    }

    /**
     * 违章详情
     */
    private void initExaminDetail() {
        Call<String> call = HttpManager.getInstance().requestService().VIOLATION_RECORD_DETAIL(id);
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                ViolationDetailOutput violationDetailOutput = new Gson().fromJson(result, ViolationDetailOutput.class);
                if (violationDetailOutput.getResult().equals("Success")) {
                    final ViolationDetailOutput.DataBean dataBean = violationDetailOutput.getData();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 承包商名称
                            for (ContractorCompanyOutput.DataBean.PageDataBean bean : MyApplication.getInstance().mListContractorCompany) {
                                if (bean.getId().equals(dataBean.getCbsListId())) {
                                    mTv_violation_name.setText(bean.getContractorName());
                                }
                            }
                            cbsListId = dataBean.getCbsListId();
                            // 违规人
                            for (ContractorPersonOutput.DataBean.PageDataBean bean : MyApplication.getInstance().mListParticipant) {
                                if (bean.getId().equals(dataBean.getViolatorsID())) {
                                    mTv_violation_person.setText(bean.getPersonnelName());
                                }
                            }
                            violatorsId = dataBean.getViolatorsID();
                            discoveringPeopleId = dataBean.getDiscoveringPeopleID();
                            // 违规条款
                            mEt_violation_clause.setText(dataBean.getViolationClause());
                            // 备注
                            mEt_violation_remark.setText(dataBean.getRemarks());
                            // 处理意见
                            mEt_violation_idea.setText(dataBean.getProcessingOpinion());
                            //黑名单
                            processingResultId = dataBean.getProcessingResult();
                            if (processingResultId.equals("1")) {
                                mTv_violation_blacklist.setText("不拉入黑名单");
                            } else if (processingResultId.equals("2")) {
                                mTv_violation_blacklist.setText("拉入黑名单");
                            }
                        }
                    });
                } else {
                    ToastUtils.getInstance().showShortToastBottom(violationDetailOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    /**
     * 黑名单
     */
    private void initBlackList() {
        DropDownInput dropDownInput = new DropDownInput();
        List<String> categoryList = new ArrayList<>();
        categoryList.add("9021");
        List<String> functionList = new ArrayList<>();
        dropDownInput.setCategoryCodeArray(categoryList);
        dropDownInput.setFunctionCodeArray(functionList);
        dropDownInput.setIsGetFactoryList(false);

        Call<String> call = HttpManager.getInstance().requestService().DROP_DOWN(HttpManager.getInstance().requestBody(dropDownInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                DropDownOutput dropDownOutput = new Gson().fromJson(result, DropDownOutput.class);
                if (dropDownOutput.getResult().equals("Success")) {
                    List<DropDownOutput.DataBean.DataDicListBean> dataDicList = dropDownOutput.getData().getDataDicList();
                    for (DropDownOutput.DataBean.DataDicListBean dataDicListBean : dataDicList) {
                        if (dataDicListBean.getKey().equals("9021")) {
                            List<DropDownOutput.DataBean.DataDicListBean.ValueBean> valueBeanList = dataDicListBean.getValue();
                            mBlackList.addAll(valueBeanList);
                        }
                    }
                } else {
                    ToastUtils.getInstance().showShortToastBottom(dropDownOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    /**
     * 承包商名称-Pop
     */
    private void initContractorCompanyPop() {
        View inflate = LayoutInflater.from(AddViolationActivity.this).inflate(R.layout.layout_common_more_department, null);
        RecyclerView mRecyclerView = (RecyclerView) inflate.findViewById(R.id.common_recyclerview);
        LinearLayoutManager llm = new LinearLayoutManager(AddViolationActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        CommonEmptyAdapter mCommonEmptyAdapter = new CommonEmptyAdapter(AddViolationActivity.this, R.layout.layout_item_common_more_department, MyApplication.getInstance().mListContractorCompany);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<ContractorCompanyOutput.DataBean.PageDataBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, ContractorCompanyOutput.DataBean.PageDataBean dataBean) {
                TextView tv_department = holder.getView(R.id.tv_department);
                tv_department.setText(dataBean.getContractorName());
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                ContractorCompanyOutput.DataBean.PageDataBean pageDataBean = MyApplication.getInstance().mListContractorCompany.get(position);
                cbsListId = pageDataBean.getId();
                mTv_violation_name.setText(pageDataBean.getContractorName());

                PopupWindowUtils.getInstance().closePop();
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
        PopupWindowUtils.getInstance().createScalePopupWindow(AddViolationActivity.this, inflate, mLl_violation_name);
    }

    /**
     * 违章人-Pop
     */
    private void initTrainPersonPop() {
        View inflate = LayoutInflater.from(AddViolationActivity.this).inflate(R.layout.layout_common_more_department, null);
        RecyclerView mRecyclerView = (RecyclerView) inflate.findViewById(R.id.common_recyclerview);
        LinearLayoutManager llm = new LinearLayoutManager(AddViolationActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        CommonEmptyAdapter mCommonEmptyAdapter = new CommonEmptyAdapter(AddViolationActivity.this, R.layout.layout_item_common_more_department, MyApplication.getInstance().mListParticipant);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<ContractorPersonOutput.DataBean.PageDataBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, ContractorPersonOutput.DataBean.PageDataBean dataBean) {
                TextView tv_department = holder.getView(R.id.tv_department);
                tv_department.setText(dataBean.getPersonnelName());
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                ContractorPersonOutput.DataBean.PageDataBean pageDataBean = MyApplication.getInstance().mListParticipant.get(position);
                violatorsId = pageDataBean.getId();
                mTv_violation_person.setText(pageDataBean.getPersonnelName());

                PopupWindowUtils.getInstance().closePop();
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
        PopupWindowUtils.getInstance().createScalePopupWindow(AddViolationActivity.this, inflate, mLl_violation_person);
    }

    /**
     * 违章日期
     */
    private void initViolationTime() {
        Calendar selectedDate = Calendar.getInstance();
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyy-MM-dd");
                mTv_violation_date.setText(sdf_ymd.format(date));
            }
        })
                .setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                .setTitleSize(20)//标题文字大小
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.WHITE)//确定按钮文字颜色
                .setCancelColor(Color.WHITE)//取消按钮文字颜色
                .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setLabel("年", "月", "日", "", "", "")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build();

        pvTime.show();
    }

    /**
     * 发现人-Pop
     */
    private void initFindPersonPop() {
        View inflate = LayoutInflater.from(AddViolationActivity.this).inflate(R.layout.layout_common_more_department, null);
        RecyclerView mRecyclerView = (RecyclerView) inflate.findViewById(R.id.common_recyclerview);
        LinearLayoutManager llm = new LinearLayoutManager(AddViolationActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        CommonEmptyAdapter mCommonEmptyAdapter = new CommonEmptyAdapter(AddViolationActivity.this, R.layout.layout_item_common_more_department, MyApplication.getInstance().mListTrainPerson);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<TrainPersonOutput.DataBean.PageDataBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, TrainPersonOutput.DataBean.PageDataBean dataBean) {
                TextView tv_department = holder.getView(R.id.tv_department);
                tv_department.setText(dataBean.getName());
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                TrainPersonOutput.DataBean.PageDataBean pageDataBean = MyApplication.getInstance().mListTrainPerson.get(position);
                discoveringPeopleId = pageDataBean.getId();
                mTv_violation_find_person.setText(pageDataBean.getName());

                PopupWindowUtils.getInstance().closePop();
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
        PopupWindowUtils.getInstance().createScalePopupWindow(AddViolationActivity.this, inflate, mLl_violation_find_person);
    }

    /**
     * 黑名单-Pop
     */
    private void initBlackListPop() {
        View inflate = LayoutInflater.from(AddViolationActivity.this).inflate(R.layout.layout_common_more_department, null);
        RecyclerView mRecyclerView = (RecyclerView) inflate.findViewById(R.id.common_recyclerview);
        LinearLayoutManager llm = new LinearLayoutManager(AddViolationActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        CommonEmptyAdapter mCommonEmptyAdapter = new CommonEmptyAdapter(AddViolationActivity.this, R.layout.layout_item_common_more_department, mBlackList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<DropDownOutput.DataBean.DataDicListBean.ValueBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, DropDownOutput.DataBean.DataDicListBean.ValueBean dataBean) {
                TextView tv_department = holder.getView(R.id.tv_department);
                tv_department.setText(dataBean.getValue());
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                DropDownOutput.DataBean.DataDicListBean.ValueBean valueBean = mBlackList.get(position);
                processingResultId = valueBean.getKey();
                mTv_violation_blacklist.setText(valueBean.getValue());
                if (valueBean.getValue().equals("不拉入黑名单")) {
                    mLl_zongjignli.setVisibility(View.GONE);
                } else {
                    mLl_zongjignli.setVisibility(View.VISIBLE);
                }
                PopupWindowUtils.getInstance().closePop();
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
        PopupWindowUtils.getInstance().createScalePopupWindow(AddViolationActivity.this, inflate, mLl_violation_blacklist);
    }

    /**
     * 签名
     */
    private void initSignRecyclerView() {
        GridLayoutManager glm = new GridLayoutManager(AddViolationActivity.this, 2);
        mRecyclerView.setLayoutManager(glm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(AddViolationActivity.this, R.layout.layout_item_sign, mListSign);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<Bitmap>() {
            @Override
            public void setItemData(CommonViewHolder holder, Bitmap bitmap) {
                ImageView mIv_violation_sign = holder.getView(R.id.iv_violation_sign);
                mIv_violation_sign.setImageBitmap(bitmap);
            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {
                mListSign.remove(position);
                mCommonEmptyAdapter.notifyDataSetChanged();
            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    /**
     * 提交
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    private void submit() {
        String company = mTv_violation_name.getText().toString();
        if (TextUtils.isEmpty(company)) {
            ToastUtils.getInstance().showShortToastBottom("请选择承包商!");
            return;
        }
        String violationPerson = mTv_violation_person.getText().toString();
        if (TextUtils.isEmpty(violationPerson)) {
            ToastUtils.getInstance().showShortToastBottom("请选择违章人!");
            return;
        }
        String violationDate = mTv_violation_date.getText().toString();
        String findPerson = mTv_violation_find_person.getText().toString();
        if (TextUtils.isEmpty(findPerson)) {
            ToastUtils.getInstance().showShortToastBottom("请输入发现人!");
            return;
        }
        String violationClause = mEt_violation_clause.getText().toString();
        if (TextUtils.isEmpty(violationClause)) {
            ToastUtils.getInstance().showShortToastBottom("请输入违规条款!");
            return;
        }
        String violationIntroduce = mEt_violation_introduce.getText().toString();
        if (TextUtils.isEmpty(violationIntroduce)) {
            ToastUtils.getInstance().showShortToastBottom("请输入违规说明!");
            return;
        }
        String remarks = mEt_violation_remark.getText().toString();
        String violationIdea = mEt_violation_idea.getText().toString();
        if (TextUtils.isEmpty(violationIdea)) {
            ToastUtils.getInstance().showShortToastBottom("请输入违规处理意见!");
            return;
        }

        AddViolationInput addViolationInput = new AddViolationInput();
        if (!TextUtils.isEmpty(id)) {
            addViolationInput.setId(id);
        }
        addViolationInput.setCbsListId(cbsListId);
        addViolationInput.setViolatorsID(violatorsId);
        addViolationInput.setViolationDate(violationDate);
        addViolationInput.setDiscoveringPeopleID(discoveringPeopleId);
        addViolationInput.setViolationClause(violationClause);
        addViolationInput.setViolationInstructions(violationIntroduce);
        if (!TextUtils.isEmpty(remarks)) {
            addViolationInput.setRemarks(remarks);
        }
        addViolationInput.setProcessingOpinion(violationIdea);
        // addViolationInput.setContractorQZ();
        addViolationInput.setProcessingResult(processingResultId);

        Call<String> call = HttpManager.getInstance().requestService().VIOLATION_RECORD_ADD(HttpManager.getInstance().requestBody(addViolationInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                CommonOutput commonOutput = new Gson().fromJson(result, CommonOutput.class);
                if (commonOutput.getResult().equals("Success")) {
                    ToastUtils.getInstance().showShortToastBottom("提交成功!");
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putInt("SUCCESS", 1);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finishActivity(AddViolationActivity.this);
                } else {
                    ToastUtils.getInstance().showShortToastBottom(commonOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bundle extras = data.getExtras();
        if (extras == null) {
            ToastUtils.getInstance().showShortToastBottom("签名取消!");
            return;
        }
        switch (requestCode) {
            case 1:
                initSignRecyclerView();
                mTv_violation_sign.setVisibility(View.GONE);
                mRecyclerView.setVisibility(View.VISIBLE);

                int bitmap_count = extras.getInt("BITMAP_COUNT");
                for (int i = 0; i < bitmap_count; i++) {
                    String bitmapStr = extras.getString("BITMAP" + i);
                    Bitmap bitmap = BitmapUtils.convertStringToBitmap(bitmapStr);
                    mListSign.add(bitmap);
                }
                mCommonEmptyAdapter.notifyDataSetChanged();
                tv_sign_time.setText(DateTimeUtils.getCurrentYMDHM());
                break;
            case Constants.REQUEST_CODE:
                ArrayList<String> images = data.getStringArrayListExtra(
                        ImageSelectorUtils.SELECT_RESULT);
                mPicList.addAll(images);
                if (mPicList.isEmpty()) {
                    mRv_pic.setVisibility(View.GONE);
                } else {
//                    mRv_pic.setVisibility(View.GONE);
//                    mCommonAdapter.notifyDataSetChanged();
                    uploadImages();
                }
                break;
        }
    }

    private void initPicRecyclerView() {
        GridLayoutManager glm = new GridLayoutManager(AddViolationActivity.this, 4);
        mRv_pic.setLayoutManager(glm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRv_pic.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRv_pic.setItemAnimator(new DefaultItemAnimator());
        mRv_pic.setNestedScrollingEnabled(false);

        mCommonAdapter = new CommonAdapter(AddViolationActivity.this, R.layout.layout_item_pic, mPicList);
        mCommonAdapter.setItemDatasListener(new CommonAdapter.ItemDatasListener<String>() {
            @Override
            public void setItemDatas(CommonViewHolder holder, String uri, int position) {
                ImageView iv_item = holder.getView(R.id.iv_item);
                Glide.with(AddViolationActivity.this).load(uri).into(iv_item);
            }
        });

        mCommonAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {

            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {
                mPicList.remove(position);
                mPicIdList.remove(position);
                mCommonAdapter.notifyDataSetChanged();
            }
        });

        mRv_pic.setAdapter(mCommonAdapter);
    }

    /**
     * 上传照片
     */
    private void uploadImages() {
        if (mPicList.isEmpty())
            return;
        for (String path : mPicList) {
            File file = new File(path);
            if (!file.exists())
                break;
            tvFujianName.setText(file.getName());
            tvFujianTime.setText(DateTimeUtils.getCurrentYMDHM());
            final RequestBody requestFile =
                    RequestBody.create(MediaType.parse("application/otcet-stream"), file);
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("files", file.getName(), requestFile);

            Call<String> call = HttpManager.getInstance().requestService().UPLOAD_FILE(body);
            call.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    UploadFileOutput uploadFileOutput = new Gson().fromJson(response.body(), UploadFileOutput.class);
                    if (uploadFileOutput.getResult().equals("Success")) {
                        String id = uploadFileOutput.getData().get(0).getId();
                        mPicIdList.add(id);
                    }
                }

                @Override
                public void onFailure(Call<String> call, Throwable t) {
                    // Log.i("Simon", "uploadImages onFailure = " + t.getMessage());
                }
            });
        }
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_OK, new Intent());
        super.onBackPressed();
    }
}
